package com.sh.annotation;

import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: sh
 * @Version 1.0
 * @Description:
 */
public class Test {
    public static void main(String[] args) {
        Map<String, Object> objectObjectHashMap = new HashMap<>();
        String packageName = "com.sh.annotation.bean";
        String path = packageName.replace(".", "/");
        URL resource = ClassLoader.getSystemClassLoader().getResource(path);
        try {
            File file = new File(resource.getPath());
            File[] files = file.listFiles();
            for (File one : files) {
                String className = packageName + "." + one.getName().split("\\.")[0];
                Class<?> aClass = Class.forName(className);
                if (aClass.isAnnotationPresent(Component.class)){
                    Component annotation = aClass.getAnnotation(Component.class);
                    String value = annotation.value();
                    Object bean = aClass.newInstance();
                    objectObjectHashMap.put(value, bean);
                }
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        for (Map.Entry<String, Object> entry : objectObjectHashMap.entrySet()) {
            System.out.println(entry.getKey() + "=" + entry.getValue());
        }
    }
}

